﻿using System.Drawing;
using System.Transactions;
namespace assignment3
{
    public abstract class Shape
    {
        public abstract int getArea();
        public abstract bool isLegal();
    }
    public class Rectangle : Shape
    {
        protected int height, width;
        public Rectangle()
        {
            height = 0;
            width = 0;
        }
        public Rectangle(int height, int width)
        {
            this.height = height;
            this.width = width;
            if (isLegal())
                if(height!=width)
                Console.WriteLine("长方形边长：{0},{1}，面积：{2}",height,width,getArea());
                else Console.WriteLine("正方形边长：{0}，面积：{1}", height, getArea());
        }

        public override int getArea()
        {
            return height * width;
        }
        public override bool isLegal()
        {
            if (height > 0 && width > 0) return true;
            return false;
        }
    }
    public class Square : Rectangle
    {
        public Square(int size) : base(size,size)
        {
        }
        public override bool isLegal()
        {
            if(height>0&&height==width) return true;
            return false;
        }
    }
    public class Triangle : Shape
    {
        private int a, b, c;
        public Triangle(int a, int b, int c)
        {
            this.a = a;
            this.b = b;
            this.c = c;
            if (isLegal())
            Console.WriteLine("三角形边长：{0},{1},{2}，面积：{3}", a, b, c,getArea());
        }

        public override int getArea()
        {
            int p = a + b + c;
            int tem = p*(p-a)*(p-b)*(p-c);
            return (int)Math.Sqrt(tem);
        }
        public override bool isLegal()
        {
            if (a > 0 && b > 0 && c > 0&&a+b>c&&b+c>a&&a+c>b)
            {
                return true;
            }
            return false;
        }
    }
    public class WrongShape : Shape
    {
        public WrongShape()
        {
           // Console.WriteLine("错误图形");
        }
        public override int getArea()
        {
            return 0;
        }
        public override bool isLegal()
        {
            return false;
        }
    }
    public class Factory
    {
        public static Shape getShape(int op,int a, int b,int c)
        {
            switch (op)
            {
                case 0: return new Square(a);
                case 1: return new Rectangle(a, b);
                case 2: return new Triangle(a, b, c);
                default: return new WrongShape();
            }
        }
    }
    public class FactoryProduce
    {
        public static void Main(String[] args)
        {
            Factory f = new Factory();
            Random rop = new Random();
            for(int i = 0,j=0; i < 10&&j<100;j++ )
            {
                if (Factory.getShape(rop.Next() % 4, rop.Next() % 20, rop.Next() % 20, rop.Next() % 20).isLegal())
                    i++;
            }
        }
    }
}